const commodity = require('../../services/commodity.js');
const cart = require('../../services/cart.js');
//获取应用实例
const app = getApp()
const wxParse = require('../../wxParse/wxParse.js');
const user = require('../../services/user.js');
const util = require('../../utils/util.js');
const order = require('../../services/order.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        info: {},
        cart: 0,
        login: false,
        userInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const id = options.id
        this.setData({
            id: id,
            login: wx.getStorageSync('login')
        })
        this.getInfo(id)
        this.getTotal()
    },

    getInfo: function (id) {
        commodity.info(id, (res) => {
            if (res.code === 0) {
                this.setData({
                    info: res.data
                })
                this.loadHtml('article', res.data.content)
            }
        })
    },

    loadHtml(bindName, content) {
        const that = this
        wxParse.wxParse(bindName, 'html', content, that, 12);
    },

    getUserInfo(e) {
        const that = this
        wx.getUserProfile({
            desc: '获取用户信息，用来快速登录',
            success: (res) => {
                that.toLogin(res)
            }
        })
    },

    toLogin(res) {
        user.wxLogin(res, (res) => {
            if (res.code == 0) {
                wx.setStorageSync('token', res.data.token)
                this.setData({
                    userInfo: res.data.userInfo,
                    login: true
                });
                wx.setStorageSync('login', true)
                wx.setStorageSync('openid', res.data.userInfo.openid)
                wx.setStorageSync('uid', res.data.userInfo.uid)
                wx.setStorageSync("userInfo", res.data.userInfo)
                app.globalData.userInfo = res.data.userInfo;
                app.globalData.token = res.data.token;
            } else {
                util.showErrorToast('登陆失败')
            }
        });
    },

    onAddCart() {
        const data = {
            cid: this.data.info.id
        }
        cart.add(data, (res) => {
            if (res.code === 0) {
                this.getTotal()
            }
        })
    },

    onToCart() {
        wx.switchTab({
            url: '../cart/cart'
        })
    },

    onToPay() {
        if (this.data.id === '') {
            wx.showModal({
                title: '请稍后再试',
                showCancel: false
            })
            return;
        }
        wx.showLoading({
            title: '提交中',
            mask: true
        })
        order.addById(this.data.id, (res) => {
            if (res.code === 0) {
                wx.navigateTo({
                    url: '../order/order?id=' + res.msg
                })
            }
        })
        wx.hideLoading()
    },

    getTotal() {
        cart.total({}, (res) => {
            if (res.code === 0) {
                this.setData({
                    cart: res.data
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
